﻿using GalaSoft.MvvmLight;
using System.Windows.Input;
using System;
using System.Linq;
using System.Collections.Generic;
using IkeCode.WP7.Wakeup.Common;
using GalaSoft.MvvmLight.Messaging;
using IkeCode.WP7.Wakeup.Service;
using IkeCode.WP7.Wakeup.AppModel.Enum;
using IkeCode.WP7.Wakeup.AppResources.Dictionaries;

namespace IkeCode.WP7.Wakeup.ViewModel
{
    public class MainViewModel : BaseViewModel
    {
        private readonly IDataService _dataService;
        private readonly INavigationService NavigationService;

        public MainViewModel(IDataService dataService, INavigationService navigationService)
        {
            _dataService = dataService;
            NavigationService = navigationService;

            _dataService.GetMainData((item, error) =>
            {
                if (error != null)
                {
                    // Report error here
                    return;
                }

                this.CurrentDaysText = item.CurrentDaysText;
            });
        }

        public override void SetData(object data)
        {
            base.SetData(data);

            Messenger.Default.Register<List<Days>>(this, "SelectedDays", (result) =>
                {
                    this.SelectedDays = result;

                    var containsMonday = result.Contains(Days.Monday);
                    var containsTuesday = result.Contains(Days.Tuesday);
                    var containsWednesday = result.Contains(Days.Wednesday);
                    var containsThursday = result.Contains(Days.Thursday);
                    var containsFriday = result.Contains(Days.Friday);
                    var containsSaturday = result.Contains(Days.Saturday);
                    var containsSunday = result.Contains(Days.Sunday);

                    if (containsSaturday && containsSunday && (!containsMonday && !containsTuesday && !containsWednesday && !containsThursday && !containsFriday))
                        this.CurrentDaysText = AppDictionary.Weekend;
                    else if (containsMonday && containsTuesday && containsWednesday && containsThursday && containsFriday && (!containsSaturday && !containsSunday))
                        this.CurrentDaysText = AppDictionary.BusinessDays;
                    else if (result.Count == 7)
                        this.CurrentDaysText = AppDictionary.AllDays;
                    else
                        this.CurrentDaysText = AppDictionary.SelectDays;
                });
        }

        #region Properties
        
        private string _currentDaysText = string.Empty;
        public string CurrentDaysText
        {
            get { return _currentDaysText; }
            set
            {
                if (_currentDaysText == value)
                    return;

                _currentDaysText = value;
                RaisePropertyChanged(() => CurrentDaysText);
            }
        }

        private List<Days> SelectedDays { get; set; }

        #endregion

        #region Commands

        public ICommand SelectDaysCommand
        {
            get
            {
                return new RelayCommand((args) =>
                {
                    this.NavigationService.NavigateTo(ViewModelLocator.SelectDaysPageUrl);
                    //Messenger.Default.Send<string>("");
                    //Messenger.Default.Register<string>(this, DoSomething);
                });
            }
        }

        public ICommand SelectRingtoneCommand
        {
            get
            {
                return new RelayCommand((args) =>
                { 
                    this.NavigationService.NavigateTo(ViewModelLocator.SelectRingtonePageUrl);
                    //Messenger.Default.Send<string>("");
                    //Messenger.Default.Register<string>(this, DoSomething);
                });
            }
        }

        #endregion
    }
}